/*
 * Copyright(c) 2018-? the original author[Wien(luweien@aliyun.com)] or authors.
 *
 */
package org.doll.it.base.exception;

import org.doll.it.base.enums.CodeEnum;

import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * 自定义异常基类
 * 
 * @author Wien(luweien@aliyun.com)
 *
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class BaseException extends RuntimeException {

	private static final long serialVersionUID = 5710675481047944335L;

	/**
	 * 状态码
	 */
	private Integer code;

	/**
	 * 描述消息
	 */
	private String message;

	public BaseException() {
		super();
	}

	public BaseException(Integer code, String msg) {
		super(msg);
		this.code = code;
		this.message = msg;
	}

	public BaseException(CodeEnum codeEnum) {
		super(CodeEnum.ofMessage(codeEnum));
		this.code = CodeEnum.ofCode(codeEnum);
		this.message = CodeEnum.ofMessage(codeEnum);
	}

	public BaseException(CodeEnum codeEnum, String extraMsg) {
		super(CodeEnum.ofMessage(codeEnum, extraMsg));
		this.code = CodeEnum.ofCode(codeEnum);
		this.message = CodeEnum.ofMessage(codeEnum, extraMsg);
	}

	public BaseException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
		super(message, cause, enableSuppression, writableStackTrace);
		this.message = message;
	}

	public BaseException(CodeEnum codeEnum, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
		super(CodeEnum.ofMessage(codeEnum), cause, enableSuppression, writableStackTrace);
		this.code = CodeEnum.ofCode(codeEnum);
		this.message = CodeEnum.ofMessage(codeEnum);
	}

	public BaseException(CodeEnum codeEnum, Throwable cause) {
		super(CodeEnum.ofMessage(codeEnum), cause);
		this.code = CodeEnum.ofCode(codeEnum);
		this.message = CodeEnum.ofMessage(codeEnum);
	}

	public BaseException(String message, Throwable cause) {
		super(message, cause);
		this.message = message;
	}

	public BaseException(CodeEnum codeEnum, String extraMsg, Throwable cause) {
		super(CodeEnum.ofMessage(codeEnum, extraMsg), cause);
		this.code = CodeEnum.ofCode(codeEnum);
		this.message = CodeEnum.ofMessage(codeEnum, extraMsg);
	}

	public BaseException(String message) {
		super(message);
		this.message = message;
	}

	public BaseException(Throwable cause) {
		super(cause);
	}

	@Override
	public String toString() {
		String className = getClass().getName();
		String message = getMessage();
		return (message != null) ? (className + ": " + message) : className;
	}

	@Override
	public String getMessage() {
		if (this.code != null && this.message != null) {
			return this.code + "-" + this.message;
		}
		if (this.message != null) {
			return this.message;
		}
		return super.getMessage();
	}

}